Yacon.dispatch.msg.TJsonBaseConverter = function(){
	
	this.reqMsg = null;
	this.rspMsg = null;
	
};

Yacon.dispatch.msg.TJsonBaseConverter.Extend(Object,{
	
	/* 打包字段 */
	compactField: function(field){
		
		var fieldName = field.getFieldName();
		var fieldValue = field.getFieldValue();
		
		var jsonValue = "";
		
		if(fieldValue == undefined){
			jsonValue = "";
		}
		
		else if(Yacon.isNull(fieldValue)){
			jsonValue = "";
		}
		
		else if(fieldValue instanceof Yacon.container.Record){
			jsonValue = this.compactRecord(fieldValue);
		}
		
		else if(fieldValue instanceof Yacon.container.Table){
			jsonValue = this.compactTable(fieldValue);
		}
		
		else if(Yacon.isString(fieldValue)){
			jsonValue = "'"+fieldValue.encode()+"'";
		}
		
		else if(fieldValue.constructor == Number){
			jsonValue = fieldValue;
		}
		
		else if(fieldValue instanceof Array){			
			jsonValue = this.compactArray(fieldValue);		
		}
		
		return "'"+fieldName+"'"+":"+jsonValue;
	},

	/* 打包表格 */
	compactTable: function(table){
		
		var jsonValue = "[";
		
		table.beforeFirst();	
		
		while(table.next()){
			
			var active = this.compactRecord(table.getRecord());
			
			jsonValue += active + ",";
			
		}
		
		if(table.getRecordCount() > 0){
			jsonValue = jsonValue.substring(0,jsonValue.length-1);
		}
			
		jsonValue += "]";
			
		return jsonValue;
	},
	
	/* 打包记录 */
	compactRecord: function(record){
		
		var jsonValue = "{";
		
		var fieldCount = record.getFieldCount();
		
		for(var i=0;i<fieldCount;i++){	
			jsonValue += this.compactField(record.getField(i)) + ",";			
		}
		
		if(fieldCount>0){
			jsonValue = jsonValue.substring(0,jsonValue.length-1);
		}
		
		jsonValue += "}";
		
		return jsonValue;
		
	},
	
	/* 打包数组 */
	compactArray: function(array){
		
		var jsonValue = "[";
		
		var size = array.length;
		
		for(var i=0;i<size;i++){
			
			var active = array[i];
			
			if(active instanceof Number){
				jsonValue += active +",";
			}
			
			else if(Yacon.isString(active)){
				jsonValue += "'"+active.encode()+"',";				
			}
			
		}
		
		if(jsonValue.length > 1){
			jsonValue = jsonValue.substring(0,jsonValue.length-1);
		}
		
		jsonValue += "]";
		
		return jsonValue;
	}
	
});